package com.itjin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itjin.model.domain.entity.PostLike;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface PostLikeMapper extends BaseMapper<PostLike> {

  @Update("INSERT INTO post_likes (postId, userId, status) VALUES (#{postId}, #{userId}, 1) " +
      "ON DUPLICATE KEY UPDATE status = IF(status = 1, 0, 1)")
  int toggleLike(@Param("postId") Integer postId, @Param("userId") Integer userId);

  /**
   * 更新用户的获赞数
   * 
   * @param userId 用户ID
   * @param delta  增减值（1或-1）
   * @return 影响行数
   */
  @Update("UPDATE user SET myPraised = myPraised + #{delta} WHERE id = #{userId}")
  int updateUserPraised(@Param("userId") Integer userId, @Param("delta") Integer delta);
}